﻿using System.Collections.Generic;
using eCommerce.core;
using eCommerce.db.DALRespository;
using eCommerce.db.DataRespository;

namespace eCommerce.db.BORespository
{
    public class CommentBo
    {        
        public static int Create(CommentEntity o)
        {
            return CommentDal.Create(o);
        }

        public static void UpdateStatus(int commentId, int status)
        {
            CommentDal.UpdateStatus(commentId, status);
        }

        public static void Delete(int commentId)
        {
            CommentDal.Delete(commentId);
        }

        public static CommentEntity GetById(int id)
        {
            return CommentDal.GetById(id);
        }

        public static int GetStatistic(int status)
        {
            return CommentDal.GetStatistic(status);
        }

        public static List<CommentEntity> GetListSeach(int typeId, int status, int pageIndex, int pageSize, ref int totalRecords)
        {
            // Nếu filter rỗng thì lấy tất cả bình luận
            //var filter = string.Empty;

            //switch (status)
            //{
            //    case (int)CommentType.IsWaitingForPublish:
            //        filter += string.Format("Status = {0}", status);
            //        break;
            //    case (int)CommentType.IsPublished:
            //        filter += string.Format("Status = {0}", status);
            //        break;
            //}

            //if (typeId > 0)
            //    filter += string.Format(status > 0 ? " AND TypeId = {0}" : "TypeId = {0}", typeId);

            //var sortBy = "CommentId DESC";

            //DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            //var lstComment = new List<CommentEntity>();
            //try
            //{
            //    var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Action",
            //                    Constant.TableComment, sortBy, Constant.AllFieldList, filter, pageIndex, pageSize);

            //    SetInfoList(ref reader, ref lstComment);

            //    totalRecords = (int)SqlHelper.ExecuteScalar(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Action_Count",
            //                            Constant.TableArticle, filter);

            //    return lstComment;
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception(string.Format(DbCommon.DatabaseSchema + "Article_Action_Pagination: {0}", ex.Message));
            //}

            //totalRecords = Action.GetListPagingCount("Comment", filter);



            //return ObjectHelper.FillCollection<CommentObject>(
            //        Action.Reader_GetListPaging("Comment", "CreatedOn DESC", filter, pageIndex, pageSize));

            return null;
        }
                

        public static List<CommentEntity> GetByParentId(int id, int parentId, int status)
        {
            return CommentDal.GetByParentId(id, parentId, status);
        }

        public static void BuilTree(int id, List<CommentEntity> arrayList, int level, int parentId, int status)
        {
            var comments = GetByParentId(id, parentId, status);

            foreach (var c in comments)
            {
                arrayList.Add(c);

                level++;
                BuilTree(id, arrayList, level, c.CommentId, status);
                level--;
            }
        }

        public static List<CommentEntity> PaginatedCollection(List<CommentEntity> comments, int pageIndex, int pageSize)
        {
            var paginatedCollection = new List<CommentEntity>();

            int windowFloor = pageIndex == 1 ? -1 : ((pageSize * pageIndex) - 1) - pageSize >= 0 ? ((pageSize * pageIndex) - 1) - pageSize : 0;

            int windowCeiling = (pageSize * pageIndex) - 1;

            foreach (var i in comments)
            {
                int a = comments.IndexOf(i);
                int b = a;
                if (comments.IndexOf(i) <= windowCeiling && comments.IndexOf(i) > windowFloor)
                    paginatedCollection.Add(i);
            }

            return paginatedCollection;
        }

        public static List<CommentEntity> PaginationComment(int id, int typeId, int pageIndex, int pageSize, ref int totalRecords)
        {
            var lstComment = new List<CommentEntity>();
            BuilTree(id, lstComment, 0, 0, (int)CommentType.IsPublished);

            totalRecords = lstComment.Count;

            var lstTemp = PaginatedCollection(lstComment, pageIndex, pageSize);
            return lstTemp;
        }

    }
}
